package com.uenpay.im.ui.old.activity.dynamic;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * @copyRight http://www.qiyunxin.com
 * @author SL
 *
 */
public class GetDynamicsInvokeItem extends BaseHttpInvokeItem {
	public class GetDynamicsResult extends HttpInvokeResult {
		public ArrayList<DynamicEntity> dynamics;
	}

	public GetDynamicsResult getOutPut() {
		return (GetDynamicsResult) GetResultObject();
	}

	public GetDynamicsInvokeItem(int page) {
		String url = APIConfiguration.getBaseUrl()
				+ "Dynamic/Dynamic/getDynamicList?pageIndex=" + page
				+ "&pageSize=12&" + APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetDynamicsResult result = new GetDynamicsResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		JSONArray array = json.optJSONArray("data");
		if (array != null) {
			result.dynamics = DynamicSerializer.deserializeDynamicEntitys(array);
		}

		return result;
	}
}
